package tests.library.ui;

import javax.swing.*;
import library.ui.*;


public class TestDialogBuilder extends DialogBuilder implements ElementListener {

	
	
	
	public TestDialogBuilder(TabContainerElement container) {
		super(null, container);
		this.setElementListener(this);
      
	}
	
	public static TabContainerElement getTestContainerModel() {
		TabContainerElement root    = new TabContainerElement("Test tab conatainer");
		ElementContainer firstTab   = new ElementContainer();
		ElementContainer secondTab  = new ElementContainer();
		
		firstTab.setName("Tab 1");
		secondTab.setName("Tab 2");
		
		createFirstTab(firstTab);
		createSecondTab(secondTab);
		
		root.add(firstTab);
		root.add(secondTab);
		
		return root;
	}
	
	static LabelElement global_test_label = null;
	protected static ElementContainer createFirstTab(ElementContainer root) {
	      ElementContainer container1 = new ElementContainer();
	      ElementContainer container2 = new ElementContainer();
	      ElementContainer container3 = new ElementContainer();
	      
	      container1.setName("Container 1");
	      container2.setName("Container 22");
	      container3.setName("Container 333");
	      
	/*** START CONTAINER 1 ***/
	      CheckBoxElement  cb1_1  = new CheckBoxElement("cb1_1", true);
	      CheckBoxElement  cb1_2  = new CheckBoxElement("cb1_2", true);
	      SeparatorElement sep1_1 = new SeparatorElement();
	      
	      
	      RadioButtonElement   rb1_1  = new RadioButtonElement("rb1_1", false);
	      RadioButtonElement   rb1_2  = new RadioButtonElement("rb1_2", true);
	      RadioButtonElement   rb1_3  = new RadioButtonElement("rb1_3", false);
	      RadioButtonElement   rb1_4  = new RadioButtonElement("rb1_4", false);
	      RadioButtonContainer rbc1_1 = new RadioButtonContainer("test rbc_1");
	      rbc1_1.showBorder(false);
	      
	      SeparatorElement   sep1_2 = new SeparatorElement();
	      TextElement        txt1_1 = new TextElement("test txt1_1");
	      SeparatorElement   sep1_3 = new SeparatorElement();
	      
	      CheckBoxElement    cb1_3  = new CheckBoxElement("cb1_3", true);
	      CheckBoxElement    cb1_4  = new CheckBoxElement("cb1_4", true);
	      CheckBoxElement    cb1_5  = new CheckBoxElement("cb1_5", true);
	      CheckBoxElement    cb1_6  = new CheckBoxElement("cb1_6", false);
	      CheckBoxElement    cb1_7  = new CheckBoxElement("cb1_7", true);
	      ElementContainer   cb_cont1 = new ElementContainer("CheckBoxConatiner");
	      
	      SeparatorElement   sep1_4 = new SeparatorElement();
	      LabelElement       lbl1_1 = new LabelElement("label 1_1 QQQQQQQQQQQQQQQWWWWWWWWWWWWWQQQQQQQQQ");
	      global_test_label         = lbl1_1;
	      
	      rbc1_1.add(rb1_1);
	      rbc1_1.add(rb1_2);
	      rbc1_1.add(rb1_3);
	      rbc1_1.add(rb1_4);
	      
	      cb_cont1.add(cb1_3);
	      cb_cont1.add(cb1_4);
	      cb_cont1.add(cb1_5);
	      cb_cont1.add(cb1_6);
	      cb_cont1.add(cb1_7);
	      
	      container1.add(cb1_1);
	      container1.add(cb1_2);
	      container1.add(sep1_1);
	      container1.add(rbc1_1);
	      container1.add(sep1_2);
	      container1.add(txt1_1);
	      container1.add(cb_cont1);
	      container1.add(sep1_4);
	      container1.add(lbl1_1);
	      

	/*** START CONTAINER 2 ***/
	      CheckBoxElement  cb2_1  = new CheckBoxElement("cb2_1", true);
	      CheckBoxElement  cb2_2  = new CheckBoxElement("cb2_2", true);
	      SeparatorElement sep2_1 = new SeparatorElement();
	      
	      
	      RadioButtonElement   rb2_1  = new RadioButtonElement("rb1_1", false);
	      RadioButtonElement   rb2_2  = new RadioButtonElement("rb1_2", true);
	      RadioButtonElement   rb2_3  = new RadioButtonElement("rb1_3", false);
	      RadioButtonElement   rb2_4  = new RadioButtonElement("rb1_4", false);
	      RadioButtonContainer rbc2_1 = new RadioButtonContainer("test rbc_1");
	      
	      
	      rbc2_1.add(rb2_1);
	      rbc2_1.add(rb2_2);
	      rbc2_1.add(rb2_3);
	      rbc2_1.add(rb2_4);
	      
	      
	      container2.add(cb2_1);
	      container2.add(cb2_2);
	      container2.add(sep2_1);
	      container2.add(rbc2_1);

	/*** START CONTAINER 3 ***/
	      TextElement txt3_1 = new TextElement("z-z-z-z-z-z-z-z-z-z");
	      txt3_1.setHorizontalScrollBar(true);
	      txt3_1.setVerticalScrollBar(true);
	      
	      container3.add(txt3_1);
	      
	      root.add(container1);
	      root.add(container2);
	      root.add(container3);

	      return root;
		}
		
		
	protected static ElementContainer createSecondTab(ElementContainer root) {
	      ElementContainer container1 = new ElementContainer();
	      ElementContainer container2 = new ElementContainer();
	      ElementContainer container3 = new ElementContainer();
	      
	      container1.setName("Container 2_1");
	      container2.setName("Container 2_22");
	      container3.setName("Container 2_333");
	      
	/*** START CONTAINER 1 ***/
	      CheckBoxElement  cb1_1  = new CheckBoxElement("cb2_1", true);
	      CheckBoxElement  cb1_2  = new CheckBoxElement("cb2_2", true);
	      SeparatorElement sep1_1 = new SeparatorElement();
	      
	      
	      RadioButtonElement   rb1_1  = new RadioButtonElement("rb2_1", false);
	      RadioButtonElement   rb1_2  = new RadioButtonElement("rb2_2", true);
	      RadioButtonElement   rb1_3  = new RadioButtonElement("rb2_3", false);
	      RadioButtonElement   rb1_4  = new RadioButtonElement("rb2_4", false);
	      RadioButtonContainer rbc1_1 = new RadioButtonContainer("test rbc_2");
	      rbc1_1.showBorder(false);
	      
	      SeparatorElement   sep1_2 = new SeparatorElement();
	      TextElement        txt1_1 = new TextElement();
	      SeparatorElement   sep1_3 = new SeparatorElement();
	      
	      CheckBoxElement    cb1_3  = new CheckBoxElement("cb2_3", true);
	      CheckBoxElement    cb1_4  = new CheckBoxElement("cb2_4", true);
	      CheckBoxElement    cb1_5  = new CheckBoxElement("cb2_5", true);
	      CheckBoxElement    cb1_6  = new CheckBoxElement("cb2_6", false);
	      CheckBoxElement    cb1_7  = new CheckBoxElement("cb2_7", true);
	      ElementContainer   cb_cont1 = new ElementContainer("CheckBoxConatiner Tab 2");
	      
	      SeparatorElement   sep1_4 = new SeparatorElement();
	      LabelElement       lbl1_1 = new LabelElement("label 2_1 SSSSSSSSSSSSSSSSSSSSSSSS");
	      
	      
	      rbc1_1.add(rb1_1);
	      rbc1_1.add(rb1_2);
	      rbc1_1.add(rb1_3);
	      rbc1_1.add(rb1_4);
	      
	      cb_cont1.add(cb1_3);
	      cb_cont1.add(cb1_4);
	      cb_cont1.add(cb1_5);
	      cb_cont1.add(cb1_6);
	      cb_cont1.add(cb1_7);
	      
	      container1.add(cb1_1);
	      container1.add(cb1_2);
	      container1.add(sep1_1);
	      container1.add(rbc1_1);
	      container1.add(sep1_2);
	      container1.add(txt1_1);
	      container1.add(cb_cont1);
	      container1.add(sep1_4);
	      container1.add(lbl1_1);
	      

	/*** START CONTAINER 2 ***/
	      CheckBoxElement  cb2_1  = new CheckBoxElement("cb2_1", true);
	      CheckBoxElement  cb2_2  = new CheckBoxElement("cb2_2", true);
	      SeparatorElement sep2_1 = new SeparatorElement();
	      
	      
	      RadioButtonElement   rb2_1  = new RadioButtonElement("rb2_1", false);
	      RadioButtonElement   rb2_2  = new RadioButtonElement("rb2_2", true);
	      RadioButtonElement   rb2_3  = new RadioButtonElement("rb2_3", false);
	      RadioButtonElement   rb2_4  = new RadioButtonElement("rb2_4", false);
	      RadioButtonContainer rbc2_1 = new RadioButtonContainer("test rbc2_1");
	      
	      
	      rbc2_1.add(rb2_1);
	      rbc2_1.add(rb2_2);
	      rbc2_1.add(rb2_3);
	      rbc2_1.add(rb2_4);
	      
	      
	      container2.add(cb2_1);
	      container2.add(cb2_2);
	      container2.add(sep2_1);
	      container2.add(rbc2_1);

	/*** START CONTAINER 3 ***/
	      TextElement txt3_1 = new TextElement("222 y-y-y-y-y-y-y-y-y-y-y-");
	      
	      container3.add(txt3_1);
	      
	      root.add(container1);
	      root.add(container2);
	      root.add(container3);

	      return root;
		}
		
	
	public static boolean isSendChanges = true;
	public void elementActionPerformed(Element element, UpdateElementListener l)
	{
	    switch(element.getElementType())
	    {
	      case Element.CHECKBOX_TYPE:
	      {
            System.out.println("Element type: CHECKBOX_TYPE");
            System.out.println("Element ID: " + element.getElementID());
            System.out.println("Element name: " + element.toString());
            System.out.println("Element state: " + ((CheckBoxElement)element).getState());
            System.out.println("");
            global_test_label.setText(global_test_label.getText() + "X");
            l.elementUpdatePerformed(global_test_label);
	      }
	      break;
	      case Element.RADIOBUTTON_TYPE:
	      {
            System.out.println("Element type: RADIOBUTTON_TYPE");
            System.out.println("Element ID: " + element.getElementID());
            System.out.println("Element name: " + element.toString());
            System.out.println("Element state: " + ((RadioButtonElement)element).getState());
            System.out.println("");
	      }
	      break;
	      case Element.TEXTEDIT_TYPE:
	      {
            System.out.println("Element type: TEXTEDIT_TYPE");
            System.out.println("Element ID: " + element.getElementID());
            System.out.println("Element name: " + element.getName());
            System.out.println("Element string: " + ((TextElement)element).getText());
            System.out.println("");
            TextElement te = (TextElement)element;
            te.setText(te.getText() + "55555577777");
            if(isSendChanges == true) {
              updateElement(te);
              isSendChanges = false;
              global_test_label.setText("Update is not allowed");
              updateElement(global_test_label);
            }
          } 
	      break;
	    }

	}
		
	
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        JDialog dialog = new TestDialogBuilder(getTestContainerModel());
        dialog.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

        //Display the window.
        dialog.pack();
        dialog.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //Turn off metal's use of bold fonts
	        UIManager.put("swing.boldMetal", Boolean.FALSE);
                createAndShowGUI();
            }
        });
    }
}
